package com.yuneec.fly.activities;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.yuneec.droneservice.ServiceManager;
import com.yuneec.droneservice.command.BaseCommand;
import com.yuneec.droneservice.command.BindAircraft;
import com.yuneec.droneservice.command.BindCamera;
import com.yuneec.droneservice.command.Calibrate;
import com.yuneec.droneservice.command.CancelUpdate;
import com.yuneec.droneservice.command.CommandListener;
import com.yuneec.droneservice.command.ExitBind;
import com.yuneec.droneservice.command.GetAircraftInfo;
import com.yuneec.droneservice.command.GetCameraInfo;
import com.yuneec.droneservice.command.GetChannelFormula;
import com.yuneec.droneservice.command.GetChannelValues;
import com.yuneec.droneservice.command.GetControllerBattery;
import com.yuneec.droneservice.command.GetControllerGPS;
import com.yuneec.droneservice.command.GetControllerSDcard;
import com.yuneec.droneservice.command.GetControllerType;
import com.yuneec.droneservice.command.GetControllerVersion;
import com.yuneec.droneservice.command.GetGpsTime;
import com.yuneec.droneservice.command.GetJoystickMap;
import com.yuneec.droneservice.command.GetJoystickValue;
import com.yuneec.droneservice.command.GetStatus;
import com.yuneec.droneservice.command.ModifyChannelValues;
import com.yuneec.droneservice.command.ScanAircraft;
import com.yuneec.droneservice.command.ScanCamera;
import com.yuneec.droneservice.command.SendMd5;
import com.yuneec.droneservice.command.SetChannelFormula;
import com.yuneec.droneservice.command.SetControllerGPS;
import com.yuneec.droneservice.command.SetJoystickMap;
import com.yuneec.droneservice.command.StartUpdate;
import com.yuneec.droneservice.command.TransFileData;
import com.yuneec.droneservice.command.UnbindAircraft;
import com.yuneec.droneservice.command.UnbindCamera;
import com.yuneec.droneservice.model.Aircraft;
import com.yuneec.droneservice.model.ControllerLocation;
import com.yuneec.droneservice.model.JoystickMap;
import com.yuneec.droneservice.model.PackageInfo;
import com.yuneec.droneservice.request.IResponse;
import com.yuneec.fly.GlobalParams;
import com.yuneec.fly.R;
import com.yuneec.fly.events.EventBusFactory;
import com.yuneec.fly.events.uiEvent.ShowLogEvent;
import com.yuneec.fly.events.uiEvent.TestResultEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;


public class CommandTestActivity extends BaseActivity implements View.OnClickListener {
    private static final int COMMANDS_TEST = 0;
    private static final int SPEED_TEST = 1;

    private boolean testing;
    private boolean recycle;
    private Button startBtn;
    private Button stopBtn;
    private Button startRecycleBtn;
    private Button startSpeedTestBtn;
    private Button stopSpeedTestBtn;
    private TextView logTV;
    private TextView countTV;
    private TextView speedTestTV;
    private long passCount;
    private long failCount;
    private long timestamp;
    private int speedSeq;
    private DecimalFormat df = new DecimalFormat("########0.00");
    PackageInfo packageInfo;
    ControllerLocation controllerLocation;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case COMMANDS_TEST:
                    if (testing) {
                        setCountTVText();
                        handler.sendEmptyMessageDelayed(COMMANDS_TEST, 1000);
                    }
                    break;
                case SPEED_TEST:
                    float time = msg.arg1 / 100f;
                    speedTestTV.setText("Time: " + df.format(time) + "    speed: " + df.format(2 * msg.arg2 / time / 1000f) + "Mb/s");
                    EventBusFactory.getUiBus().post(new ShowLogEvent("progress " + msg.arg2 / 100 + "%\t    size: " + df.format(2 * msg.arg2 / 1000f) + "Mb\t    speed: " + df.format(2 * msg.arg2 / time / 1000f) + "Mb/s"));
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_command_test);
        GlobalParams.stopAllCommand = true;
        startBtn = (Button) findViewById(R.id.startBtn);
        stopBtn = (Button) findViewById(R.id.stopBtn);
        startRecycleBtn = (Button) findViewById(R.id.startRecycleBtn);
        startSpeedTestBtn = (Button) findViewById(R.id.startSpeedTestBtn);
        stopSpeedTestBtn = (Button) findViewById(R.id.stopSpeedTestBtn);
        logTV = (TextView) findViewById(R.id.logTV);
        countTV = (TextView) findViewById(R.id.countTV);
        speedTestTV = (TextView) findViewById(R.id.speedTestTV);
        startBtn.setOnClickListener(this);
        startRecycleBtn.setOnClickListener(this);
        stopBtn.setOnClickListener(this);
        startSpeedTestBtn.setOnClickListener(this);
        stopSpeedTestBtn.setOnClickListener(this);
        if (!EventBusFactory.getUiBus().isRegistered(this)) {
            EventBusFactory.getUiBus().register(this);
        }

        packageInfo = new PackageInfo();
        packageInfo.setFilename("update.lzo");
        packageInfo.setHardwareVersion("9.9.9");
        packageInfo.setMcuVersion("9.9.9");
        packageInfo.setFirmwareVersion("9.9.9");
        packageInfo.setType("st10c");
        packageInfo.setFileSize(1024 * 1024 * 5);

        controllerLocation = new ControllerLocation();
        controllerLocation.longitude = 120.111;
        controllerLocation.latitude = 32.111;
        controllerLocation.altitude = 88;
        controllerLocation.satelliteCount = 5;
        controllerLocation.accuracy = 6;
        controllerLocation.angle = 7;
        controllerLocation.speed = 8;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.startBtn:
                recycle = false;
                onStartCommandsTest();
                break;
            case R.id.startRecycleBtn:
                recycle = true;
                onStartCommandsTest();
                break;
            case R.id.stopBtn:
                onStopCommandsTest();
                break;
            case R.id.startSpeedTestBtn:
                onStartSpeedTest();
                break;
            case R.id.stopSpeedTestBtn:
                onStopSpeedTest();
                break;
        }
    }

    private void onStartCommandsTest() {
        timestamp = SystemClock.elapsedRealtime();
        handler.sendEmptyMessageDelayed(COMMANDS_TEST, 1000);
        passCount = 0;
        failCount = 0;
        startBtn.setEnabled(false);
        startRecycleBtn.setEnabled(false);
        stopBtn.setEnabled(true);
        startSpeedTestBtn.setEnabled(false);
        stopSpeedTestBtn.setEnabled(false);
        testing = true;
        test1();
    }

    private void onStopCommandsTest() {
        startBtn.setEnabled(true);
        startRecycleBtn.setEnabled(true);
        stopBtn.setEnabled(false);
        startSpeedTestBtn.setEnabled(true);
        testing = false;
        setCountTVText();
        handler.removeMessages(COMMANDS_TEST);
    }

    private void onStartSpeedTest() {
        timestamp = SystemClock.elapsedRealtime();
//        handler.sendEmptyMessageDelayed(0,1000);
//        passCount=0;
//        failCount =0;
        startBtn.setEnabled(false);
        startRecycleBtn.setEnabled(false);
        stopBtn.setEnabled(false);
        startSpeedTestBtn.setEnabled(false);
        stopSpeedTestBtn.setEnabled(true);
        testing = true;
        speedSeq = 0;
        logTV.setText("start transfer speed test ...");
//        sendCommand(new StartUpdate(packageInfo), new TestListener() {
//            @Override
//            public void onPass() {
//                if(testing){
//                    speedTest();
//                }
//            }
//
//            @Override
//            public void onUnPass() {
//                EventBusFactory.getUiBus().post(new ShowLogEvent("Transfer Speed fail !"));
//                handler.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        onStopSpeedTest();
//                    }
//                });
//            }
//        });`
        speedTest();
    }

    private void speedTest() {
        sendCommand(new TransFileData(speedSeq, new byte[2 * 1024]), new TestListener() {
            @Override
            public void onPass() {
                if (testing) {
                    speedSeq++;
                    if (speedSeq < 10000) {
                        if (speedSeq % 100 == 0) {
                            int time = (int) ((SystemClock.elapsedRealtime() - timestamp) / 10);
                            handler.obtainMessage(SPEED_TEST, time, speedSeq).sendToTarget();
                        }
                        speedTest();
                    } else {
                        int time = (int) ((SystemClock.elapsedRealtime() - timestamp) / 10);
                        handler.obtainMessage(SPEED_TEST, time, speedSeq).sendToTarget();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                onStopSpeedTest();
                            }
                        });
                    }
                }
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new ShowLogEvent("Transfer Speed fail !"));
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onStopSpeedTest();
                    }
                });
            }
        });
    }

    private void onStopSpeedTest() {
        startBtn.setEnabled(true);
        startRecycleBtn.setEnabled(true);
        startSpeedTestBtn.setEnabled(true);
        stopSpeedTestBtn.setEnabled(false);
        testing = false;
//        setCountTVText();
//        handler.removeMessages(0);
    }

    private void setCountTVText() {
        countTV.setText("Time: " + getTimeString() + "      " + passCount + " pass  ,  " + failCount + " fail");
    }

    private String getTimeString() {
        String s = "";
        int total = (int) ((SystemClock.elapsedRealtime() - timestamp) / 1000);
        int seconds = total % 60;
        int minutes = (total % 3600 - seconds) / 60;
        int hours = (total - minutes * 60 - seconds) / 3600;
        if (hours < 10) {
            s += "0" + hours;
        } else {
            s += hours;
        }
        s += ":";
        if (minutes < 10) {
            s += "0" + minutes;
        } else {
            s += minutes;
        }
        s += ":";
        if (seconds < 10) {
            s += "0" + seconds;
        } else {
            s += seconds;
        }
        return s;
    }

    @Override
    protected void onDestroy() {
        GlobalParams.stopAllCommand = false;
        if (EventBusFactory.getUiBus().isRegistered(this)) {
            EventBusFactory.getUiBus().unregister(this);
        }
        super.onDestroy();
    }

    int logCount = 0;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShowLog(ShowLogEvent event) {
        if (logCount > 300) {
            logCount = 0;
            logTV.setText(event.msg);
        } else {
            logTV.setText(event.msg + "\r\n" + logTV.getText());
//            logTV.setText(logTV.getText() + "\r\n" + event.msg);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTestResultEvent(TestResultEvent event) {
        EventBusFactory.getUiBus().post(new ShowLogEvent(event.id + "\t\t" + event.command + "\t\t " + (event.pass ? "pass" : "fail")));
        if (event.pass == false) {
            failCount++;
        } else {
            passCount++;
        }
        if (!testing) {
            return;
        }
        switch (event.id) {
            case 1:
                test2();
                break;
            case 2:
                test3();
                break;
            case 3:
                test4();
                break;
            case 4:
                test5();
                break;
            case 5:
                test6();
                break;
            case 6:
                test7();
                break;
            case 7:
                test8();
                break;
            case 8:
                test9();
                break;
            case 9:
                test10();
                break;
            case 10:
                test11();
                break;
            case 11:
                test12();
                break;
            case 12:
                test13();
                break;
            case 13:
                test14();
                break;
            case 14:
                test15();
                break;
            case 15:
                test16();
                break;
            case 16:
                test17();
                break;
            case 17:
                test18();
                break;
            case 18:
                test19();
                break;
            case 19:
                test20();
                break;
            case 20:
                test21();
                break;
            case 21:
                test22();
                break;
            case 22:
                test23();
                break;
            case 23:
                test24();
                break;
            case 24:
                test25();
                break;
            case 25:
                test26();
                break;
            case 26:
                test27();
                break;
            case 27:
                test28();
                break;
            case 28:
                test29();
                break;
            case 29:
                test30();
                break;
            case 30:
                test31();
                break;
            case 31:
                if (recycle) {
                    test1();
                } else {
                    onStopCommandsTest();
                }
                break;
        }
    }

    private void test1() {
        logTV.setText("start test，totally 31 commands");
        sendCommand(new GetControllerGPS(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(1, "GetControllerGPS", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(1, "GetControllerGPS", false));
            }
        });
    }

    private void test2() {
        sendCommand(new GetControllerSDcard(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(2, "GetControllerSDcard", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(2, "GetControllerSDcard", false));
            }
        });

    }

    private void test3() {
        sendCommand(new GetControllerBattery(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(3, "GetControllerBattery", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(3, "GetControllerBattery", false));
            }
        });
    }

    private void test4() {
        sendCommand(new GetChannelValues(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(4, "GetChannelValues", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(4, "GetChannelValues", false));
            }
        });
    }

    private void test5() {
        sendCommand(new GetGpsTime(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(5, "GetGpsTime", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(5, "GetGpsTime", false));
            }
        });
    }

    private void test6() {
        sendCommand(new ModifyChannelValues(1, 0, 0), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(6, "ModifyChannelValues", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(6, "ModifyChannelValues", false));
            }
        });
    }

    private void test7() {
//        sendCommand(new SendPayloadBypass(new byte[5]), new TestListener() {
//            @Override
//            public void onPass() {
//                EventBusFactory.getUiBus().post(new TestResultEvent(7, "SendPayloadBypass", true));
//            }
//
//            @Override
//            public void onUnPass() {
//                EventBusFactory.getUiBus().post(new TestResultEvent(7, "SendPayloadBypass", false));
//            }
//        });
        Toast.makeText(this, "this test7 is not support now!", Toast.LENGTH_SHORT).show();
    }

    private void test8() {
        sendCommand(new GetJoystickValue(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(8, "GetJoystickValue", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(8, "GetJoystickValue", false));
            }
        });
    }

    private int[] formula;
    private int[] defaultFormula = new int[]{683, 854, 1025, 1196, 1367, 1538, 1709, 1880, 2048, 2217, 2388, 2559, 2730, 2901, 3072, 3243, 3414};

    private void test9() {
        sendCommand(new GetChannelFormula(1), new CommandListener<int[]>() {
            @Override
            public void onSuccess(IResponse<int[]> response) {
                formula = response.getParam();
                int sum = 0;
                for (int i : formula) {
                    sum += i;
                }
                if (sum == 0) {
                    formula = defaultFormula;
                }
                EventBusFactory.getUiBus().post(new TestResultEvent(9, "GetChannelFormula", true));
            }

            @Override
            public void onTimeout() {
                formula = defaultFormula;
                EventBusFactory.getUiBus().post(new TestResultEvent(9, "GetChannelFormula", false));
            }

            @Override
            public void onError(int errorCode) {
                formula = defaultFormula;
                EventBusFactory.getUiBus().post(new TestResultEvent(9, "GetChannelFormula", true));
            }
        });
    }

    private void test10() {
        sendCommand(new SetChannelFormula(1, formula), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(10, "SetChannelFormula", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(10, "SetChannelFormula", false));
            }
        });
    }

    private JoystickMap joystickMap;

    private void test11() {
        sendCommand(new GetJoystickMap(), new CommandListener<JoystickMap>() {
            @Override
            public void onSuccess(IResponse<JoystickMap> response) {
                joystickMap = response.getParam();
                EventBusFactory.getUiBus().post(new TestResultEvent(11, "GetJoystickMap", true));
            }

            @Override
            public void onTimeout() {
                joystickMap = new JoystickMap(1, 2, 3, 4);
                EventBusFactory.getUiBus().post(new TestResultEvent(11, "GetJoystickMap", false));
            }

            @Override
            public void onError(int errorCode) {
                joystickMap = new JoystickMap(1, 2, 3, 4);
                EventBusFactory.getUiBus().post(new TestResultEvent(11, "GetJoystickMap", true));
            }
        });
    }

    private void test12() {
        sendCommand(new SetJoystickMap(joystickMap), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(12, "SetJoystickMap", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(12, "SetJoystickMap", false));
            }
        });
    }

    private Aircraft[] aircrafts;

    private void test13() {
        sendCommand(new ScanAircraft(), new CommandListener<Aircraft[]>() {
            @Override
            public void onSuccess(IResponse<Aircraft[]> response) {
                aircrafts = response.getParam();
                EventBusFactory.getUiBus().post(new TestResultEvent(13, "ScanAircraft", true));
            }

            @Override
            public void onTimeout() {
                EventBusFactory.getUiBus().post(new TestResultEvent(13, "ScanAircraft", false));
                sendCommand(new ExitBind());
            }

            @Override
            public void onError(int errorCode) {
                EventBusFactory.getUiBus().post(new TestResultEvent(13, "ScanAircraft", true));
                sendCommand(new ExitBind());
            }

        });
    }

    private void test14() {
        int id = 1;
        if (aircrafts != null && aircrafts.length > 0) {
            id = aircrafts[0].getId();
        }
        sendCommand(new BindAircraft(id), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(14, "BindAircraft", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(14, "BindAircraft", false));
            }

        });
    }

    private void test15() {
        sendCommand(new GetAircraftInfo(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(15, "GetAircraftInfo", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(15, "GetAircraftInfo", false));
            }
        });
    }

    private void test16() {
        sendCommand(new UnbindAircraft(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(16, "UnbindAircraft", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(16, "UnbindAircraft", false));
            }
        });
    }

    private void test17() {
        sendCommand(new ExitBind(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(17, "ExitBind", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(17, "ExitBind", false));
            }
        });
    }

    private void test18() {
        sendCommand(new ScanCamera(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(18, "ScanCamera", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(18, "ScanCamera", false));
            }

        });
    }

    private void test19() {
        String ssid = "miwifi_5G";
        String pass = "0987654321";
        sendCommand(new BindCamera(ssid, pass), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(19, "BindCamera", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(19, "BindCamera", false));
            }

            @Override
            public long getTimeout() {
                return 30000;
            }
        });
    }

    private void test20() {
        sendCommand(new GetCameraInfo(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(20, "GetCameraInfo", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(20, "GetCameraInfo", false));
            }
        });
    }

    private void test21() {
        sendCommand(new UnbindCamera(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(21, "UnbindCamera", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(21, "UnbindCamera", false));
            }
        });
    }

    private void test22() {
        sendCommand(new GetControllerType(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(22, "GetControllerType", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(22, "GetControllerType", false));
            }
        });
    }

    private void test23() {
        sendCommand(new GetControllerVersion(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(23, "GetControllerVersion", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(23, "GetControllerVersion", false));
            }
        });
    }

    private void test24() {
        sendCommand(new StartUpdate(packageInfo), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(24, "StartUpdate", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(24, "StartUpdate", false));
            }
        });
    }

    private void test25() {
        sendCommand(new TransFileData(0, new byte[1024]), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(25, "TransFileData", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(25, "TransFileData", false));
            }
        });
    }

    private void test26() {
        sendCommand(new GetStatus(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(26, "GetStatus", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(26, "GetStatus", false));
            }
        });
    }

    private void test27() {
        sendCommand(new SendMd5("4621D373CADE4E83"), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(27, "SendMd5", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(27, "SendMd5", false));
            }
        });
    }

    private void test28() {
        sendCommand(new CancelUpdate(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(28, "CancelUpdate", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(28, "CancelUpdate", false));
            }
        });
    }

    private void test29() {
        sendCommand(Calibrate.startCalibrateCmd(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(29, "StartCalibrate", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(29, "StartCalibrate", false));
            }
        });
    }

    private void test30() {
        sendCommand(Calibrate.stopCalibrateCmd(), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(30, "StopCalibrate", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(30, "StopCalibrate", false));
            }
        });
    }

    private void test31() {

        sendCommand(new SetControllerGPS(true, controllerLocation), new TestListener() {
            @Override
            public void onPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(31, "SetControllerGPS", true));
            }

            @Override
            public void onUnPass() {
                EventBusFactory.getUiBus().post(new TestResultEvent(31, "SetControllerGPS", false));
            }
        });
    }

    private void sendCommand(BaseCommand command) {
        sendCommand(command, null);
    }

    private void sendCommand(BaseCommand command, CommandListener listener) {
        ServiceManager.getInstance().sendDroneCommand(command, listener);
    }

    abstract class TestListener extends CommandListener {
        public abstract void onPass();

        public abstract void onUnPass();


        @Override
        public void onSuccess(IResponse response) {
            onPass();
        }

        @Override
        public void onError(int errorCode) {
            EventBusFactory.getUiBus().post(new ShowLogEvent("next logged command onError , code " + errorCode));
            onPass();
        }

        @Override
        public void onTimeout() {
            onUnPass();
        }
    }
}
